package com.toKnow.service.impl;

import com.toKnow.mapper.SearchHistoryMapper;
import com.toKnow.mapper.SourceMapper;
import com.toKnow.mapper.ColumnMapper;
import com.toKnow.mapper.UserInfoMapper;
import com.toKnow.model.dto.SearchDto;
import com.toKnow.model.entity.Source;
import com.toKnow.model.entity.Source;
import com.toKnow.model.entity.SourceAnswer;
import com.toKnow.model.entity.SourceComment;
import com.toKnow.model.vo.RestrictionVo;
import com.toKnow.service.ServiceException;
import com.toKnow.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SourceServiceImpl implements SourceService {
    @Autowired
    SourceMapper sourceMapper;
    @Autowired
    ColumnMapper columnMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    SearchHistoryMapper historyMapper;

    @Override
    public Source getSource(Integer id) {
        if (id==null) return null;
        Source Source = sourceMapper.selectById(id);
        if (Source==null) return null;
        Source.setTagList(sourceMapper.selectTagById(id));
        Source.setTypeList(sourceMapper.selectTypeById(id));
        return Source;
    }

    @Override
    public Boolean downloadSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        Source source = sourceMapper.selectById(sourceId);
        if (source==null) throw new ServiceException("没有找到资源");
        Integer authorId=source.getAuthorId();
        userInfoMapper.updateDownloadTimes(authorId,1);
        sourceMapper.updateDownloadNumber(sourceId,1);
        sourceMapper.insertUserDownload(userId, sourceId);
        return true;
    }

    @Override
    public Boolean buySource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        if (isBuySource(userId, sourceId)) throw new ServiceException("您已经购买过此资源");
        sourceMapper.updateBuyNumber(sourceId,1);
        Source source = sourceMapper.selectById(sourceId);
        sourceMapper.insertUserBuy(userId,sourceId,source.getPrice());
        return true;
    }

    @Override
    @Transactional
    public Boolean changeLikeSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
//        List<Integer> columnIds = columnMapper.selectColumnBySourceId(sourceId);
        if (isLikeSource(userId, sourceId)){
            if(!sourceMapper.updateLikeNumber(sourceId,-1)){
                throw new ServiceException("没有找到资源");
            }
//            for (Integer id : columnIds){
//                columnMapper.updateLikeNumber(id,-1);
//            }
            return sourceMapper.deleteUserLike(userId, sourceId);
        }else{
            if(!sourceMapper.updateLikeNumber(sourceId,1)){
                throw new ServiceException("没有找到资源");
            }
//            for (Integer id : columnIds){
//                columnMapper.updateLikeNumber(id,1);
//            }
            return sourceMapper.insertUserLike(userId, sourceId);
        }
    }

    @Override
    @Transactional
    public Boolean changeCollectSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        Source source = sourceMapper.selectById(sourceId);
        if (source==null) throw new ServiceException("没有找到资源");
        Integer authorId=source.getAuthorId();
        if (isCollectSource(userId, sourceId)){
            if(!sourceMapper.updateCollectNumber(sourceId,-1)){
                throw new ServiceException("没有找到资源");
            }
            userInfoMapper.updateCollectTimes(source.getAuthorId(),-1);
            return sourceMapper.deleteUserCollect(userId, sourceId);
        }else{
            if(!sourceMapper.updateCollectNumber(sourceId,1)){
                throw new ServiceException("没有找到资源");
            }
            userInfoMapper.updateCollectTimes(source.getAuthorId(),1);
            return sourceMapper.insertUserCollect(userId, sourceId);
        }
    }

    @Override
    public Boolean isCollectSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        return sourceMapper.checkUserCollect(userId,sourceId);
    }

    @Override
    public Boolean isLikeSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        return sourceMapper.checkUserLike(userId,sourceId);
    }

    @Override
    public Boolean isBuySource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        Source source = sourceMapper.selectById(sourceId);
        if (source==null) throw new ServiceException("没有找到该资源");
        if (!source.getCharged()) return true;
        return sourceMapper.checkUserBuy(userId,sourceId);
    }

    @Override
    public Boolean isDownloadSource(Integer userId, Integer sourceId) {
        if (userId==null||sourceId==null) return false;
        return sourceMapper.checkUserDownload(userId,sourceId);
    }
    @Transactional(readOnly = false)
    @Override
    public List<Source> searchSource(Integer uid,RestrictionVo restrictionVo) {
        String content=restrictionVo.getKeyword();
        if (content!=null&&uid!=null) historyMapper.insertSearchHistory(uid,content);
        return sourceMapper.selectSourceBySearch(new SearchDto(restrictionVo,"source"));
    }

    @Override
    public List<SourceComment> getComment(Integer sourceId, Integer page) {
        if (sourceId==null) throw new ServiceException("没有携带资源编号");
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return sourceMapper.selectSourceCommentBySourceId(sourceId,(page-1)*10);
    }

    @Override
    public List<SourceAnswer> getAnswer(Integer commentId, Integer page) {
        if (commentId==null) throw new ServiceException("没有携带评论编号");
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return sourceMapper.selectSourceAnswerByCommentId(commentId,(page-1)*10);
    }

    @Override
    public List<Source> getDraftSource(Integer uid) {
        return sourceMapper.getDraftSource(uid);
    }

    @Override
    public List<Source> getCheckingSource(Integer uid) {
        return sourceMapper.getCheckingSource(uid);
    }

    @Override
    public Source selectSourceDraftById(Integer uid, Integer sid) {
        Source source = sourceMapper.selectSourceDraftById(uid, sid);
        if(source == null) return null;
        source.setTagList(sourceMapper.selectDraftTagById(sid));
        source.setTypeList(sourceMapper.selectDraftTypeById(sid));
        return source;
    }
}
